package com.easylinkin.linkappapi.grid.controller;

import cn.hutool.core.util.ObjectUtil;
import com.easylinkin.linkappapi.grid.entity.GridAppraiseCompany;
import com.easylinkin.linkappapi.grid.models.dto.GridAppraiseCompanyScoreDTO;
import com.easylinkin.linkappapi.grid.service.IGridAppraiseCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;


/**
 * 网格评比单位表 前端控制器
 *
 * @author
 * @since 2024-05-16
 */
@Api(tags = "网格评比单位表")
@RestController
@RequestMapping("/grid/gridAppraiseCompany")
public class GridAppraiseCompanyController {
    @Autowired
    private IGridAppraiseCompanyService gridAppraiseCompanyService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping(value = "/list/{appraiseId}")
    public RestMessage queryList(@PathVariable("appraiseId") Integer appraiseId) {
        return gridAppraiseCompanyService.queryList(appraiseId);
    }

    /**
     * 保存
     *
     * @param gridAppraiseCompany
     * @return
     */
    @ApiOperation(value = "填写分数", notes = "填写分数")
    @PostMapping(value = "/fillOutScore")
    public RestMessage fillOutScore(@Validated @RequestBody GridAppraiseCompanyScoreDTO gridAppraiseCompany) {
        return gridAppraiseCompanyService.fillOutScore(gridAppraiseCompany);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @GetMapping(value = "/delete/{id}")
    public RestMessage delete(@PathVariable("id") Integer id) {
        GridAppraiseCompany byId = this.gridAppraiseCompanyService.getById(id);
        if (ObjectUtil.isNull(byId)) {
            return RestBuilders.successBuilder().message("数据不存在").build();
        }
        gridAppraiseCompanyService.removeById(id);
        gridAppraiseCompanyService.refreshCompanyCount(byId.getAppraiseId());
        return RestBuilders.successBuilder().message("删除成功").build();
    }

}
